package com.baseeasy.versionupdatelib;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;


/**
 *  Created by Mr.Zan on 2017/12/27.
 */

public class VersionUpdatedUtils {
    private static SelfDialog selfDialog;
    private static String BANBENHAO = "";//被忽略的版本号
    private static boolean isFirst;//判断发现新版本后是否是第一次弹出升级框

    /**
     * @param versionCode   版本号
     * @param downLoadUrl   下载链接
     * @param isForceUpdate 1 代表强制升级，0 代表非强制升级
     * @param updateInfo    更新信息
     */
    public static void isVersionUpdated(final Activity context ,final String versionCode, final String downLoadUrl, String isForceUpdate, final String updateInfo,String fileName,String applicationId) {
        //保存从网上获取的serviceCode
        SPHelper.getInst(context).saveString("serviceCode", versionCode);
        double serviceCode = Double.parseDouble(versionCode);
        //判断是否忽略过版本
        BANBENHAO = SPHelper.getInst(context).getString("BANBENHAO");
        if (!BANBENHAO.equals("")) {

            double SERVICECD = Double.parseDouble(BANBENHAO);
            if (SERVICECD < serviceCode) {
                isFirst = true;
            } else {
                isFirst = false;
            }
            SPHelper.getInst(context).saveBoolean("isFirst", isFirst);
        } else {
            isFirst = true;
            BANBENHAO = versionCode;
            SPHelper.getInst(context).saveBoolean("isFirst", isFirst);
        }
        //判断发现新版本后是否是第一次弹出升级框
        isFirst = SPHelper.getInst(context).getBoolean("isFirst");
        double code = getVersionCode(context);
        //判断是否需要版本升级
        if (code != serviceCode && code < serviceCode) {
            SPHelper.getInst(context).saveString("downLoadUrl", downLoadUrl);
            if (isFirst || isForceUpdate.equals("1")) {
                selfDialog = new SelfDialog(context, R.style.dialog, updateInfo);
                selfDialog.show();
                selfDialog.setYesOnclickListener("立即升级", new SelfDialog.onYesOnclickListener() {
                    @Override
                    public void onYesClick() {
                        new UpdateManager(context, context, downLoadUrl,fileName,applicationId);
                        selfDialog.dismiss();
                    }
                });

                //若强制升级显示
                if (isForceUpdate.equals("1")) {
                    selfDialog.setNoOnclickListener("退出", new SelfDialog.onNoOnclickListener() {
                        @Override
                        public void onNoClick() {
                            selfDialog.dismiss();
                            context.finish();
                        }
                    });
                } else if (isForceUpdate.equals("0")) {
                    //若非强制升级时显示
                    selfDialog.setNoOnclickListener("忽略此次", new SelfDialog.onNoOnclickListener() {
                        @Override
                        public void onNoClick() {
                            isFirst = false;
                            SPHelper.getInst(context).saveBoolean("isFirst", isFirst);
                            //保存到本地
                            BANBENHAO = versionCode;
                            SPHelper.getInst(context).saveString("updateDes", updateInfo);
                            SPHelper.getInst(context).saveString("BANBENHAO", BANBENHAO);
                            selfDialog.dismiss();
                        }
                    });
                }
            }

        }
    }
    public static int getVersionCode(Context context) {
        int versionCode = 0;
        try {
            versionCode =context
                    .getPackageManager()
                    .getPackageInfo(context.getPackageName(),
                            0).versionCode;
        } catch (PackageManager.NameNotFoundException ex) {
            versionCode = 0;
        }
        return versionCode;
    }
}
